package com.ccccat.www.msgs.service;



import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import com.ccccat.www.msgs.Receiver.AlarmRecevier;
import com.ccccat.www.msgs.Receiver.PhoneReceiver;

public class WakeLockService extends Service{
private  static String TAG="WakeLockService";
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
    @Override
    	public void onCreate() {
    		// TODO Auto-generated method stub
    		super.onCreate();
    	}

      @Override
    	public int onStartCommand(Intent mintent, int flags, int startId) {
    		// TODO Auto-generated method stub 	      
    	           Intent intent=new Intent(this,AlarmRecevier.class);
    	           intent.setAction("sendbroadcast.action");
    	           sendBroadcast(intent);
    	           PendingIntent sender=PendingIntent.getBroadcast(this, 0, intent, 0);
    				//从开机完成时开始计时
    				long fristtume=SystemClock.elapsedRealtime();
    				//得到全局定时器
    				AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    				//毎30秒发个广播
    				am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, fristtume, 10*1000, sender);
    				Log.i(TAG, "10秒发送一个广播！");

        return  super.onStartCommand(mintent, flags, startId);
    	}
      @Override
    	public void onDestroy() {
    		// TODO Auto-generated method stub
    	  stopForeground(true);
    	  Intent intent=new Intent(this,PhoneReceiver.class);
    	  intent.setAction("android.intent.relodingservice");
    	  sendBroadcast(intent);
    	  Intent protectintent=new Intent(this,Protectservice.class);
    	  //保护启动服务
      	  this.startService(protectintent);
    	  super.onDestroy();
    	}
      @Override
    	public boolean stopService(Intent name) {
    		// TODO Auto-generated method stub
    	  Intent intent=new Intent(this,PhoneReceiver.class);
    	  intent.setAction("android.intent.restartservice");
    	  sendBroadcast(intent);
    	return super.stopService(name);
    	}
}
